<?php

namespace App\Http\Controllers\Client;

use App\Http\Controllers\Controller;
use App\Models\yb\GoodsModel;
use App\Models\yb\GoodsTypeModel;
use App\Models\yb\NetworkGoodsModel;
use App\Models\yb\ShoppingCartModel;
use App\Models\yb\UserModel;
use Illuminate\Http\Request;


class ShoppingCartController extends Controller
{
    /**
     * Get a JWT via given credentials.
     *  加入购物车
     * @return \Illuminate\Http\JsonResponse
     */

    public function Add(Request $request)
    {
        $openid = $request->post('openid');
        $user_data = UserModel::where(['openid'=>$openid])->first();
        if(empty($user_data)){
            return response()->json(['code'=>'401','msg' => '请登录']);
        }

        $goods_id = $request->post('goods_id');

        if(empty($goods_id)){
            return response()->json(['code'=>'201','msg' => '请选择商品']);
        }

        $network_id = $request->post('network_id');

        if(empty($network_id)){
            return response()->json(['code'=>'201','msg' => '请选择门店']);
        }
        
        $number= $request->post('number');

        if(empty($number)){
            return response()->json(['code'=>'201','msg' => '请选择加入购物车数量']);
        }
        
        $shop_car_data = ShoppingCartModel::where(['user_id'=>$user_data['id'],'goods_id'=>$goods_id,'network_id'=>$network_id,'goods_id'=>$goods_id])->first();
        
        if(empty($shop_car_data)){
            ShoppingCartModel::insert([
                'user_id'=>$user_data['id'],
                'openid'=>$openid,
                'network_id'=>$network_id,
                'number' => $number,
                'goods_id'=>$goods_id,
                'created_at'=>date('Y-m-d H:i:s',time())
            ]);
        }else{
            ShoppingCartModel::where(['user_id'=>$user_data['id'],'goods_id'=>$goods_id,'network_id'=>$network_id,'goods_id'=>$goods_id])->increment('number',$number);
        }
      
        return response()->json(['code'=>'200','msg' => '请求成功']);

    }
    
      /**
     * Get a JWT via given credentials.
     *  删除购物车
     * @return \Illuminate\Http\JsonResponse
     */

    public function Del(Request $request)
    {
        $openid = $request->post('openid');
        $user_data = UserModel::where(['openid'=>$openid])->first();
        if(empty($user_data)){
            return response()->json(['code'=>'401','msg' => '请登录']);
        }
        
        $shopping_car_id = $request->post('shopping_car_id');

        if(empty($shopping_car_id)){
            return response()->json(['code'=>'201','msg' => '请选择要删除的']);
        }

        ShoppingCartModel::whereIn('id',json_decode($shopping_car_id,true))->delete();
    
    
        return response()->json(['code'=>'200','msg' => '请求成功']);

    }

    /**
     * Get a JWT via given credentials.
     *  购物车列表
     * @return \Illuminate\Http\JsonResponse
     */

    public function ShoppingList(Request $request)
    {

        $openid = $request->post('openid');
        $user_data = UserModel::where(['openid'=>$openid])->first();
        if(empty($user_data)){
            return response()->json(['code'=>'401','msg' => '请登录']);
        }

        $page = $request->post('page', 1);

        $take = $request->post('take', 10);
        
        $network_id = $request->post('network_id');

        if(empty($network_id)){
            return response()->json(['code'=>'201','msg' => '请选择收货地址']);
        }

        $data = ShoppingCartModel::where(['user_id'=>$user_data['id'],'network_id'=>$network_id])->orderBy('created_at','desc')->skip(($page - 1) * $take)->take($take)->get();

        foreach($data as $key=>$val){
            $network_goods_data = NetworkGoodsModel::where(['network_id'=>$val['network_id'],'goods_id'=>$val['goods_id']])->skip(($page - 1) * $take)->take($take)->first();
            $goods_data = GoodsModel::where(['id'=>$val['goods_id']])->first();
            
            $goods_data['goods_cover'] = env('OSS_URL').$goods_data['goods_cover'];
            $goods_data['goods_type_name'] = GoodsTypeModel::selectValue('id',$goods_data['goods_type']);
            $goods_data['network_goods_id'] = $val['id'];
            
            if($network_goods_data['is_fictitious'] == 1){
                $goods_data['goods_sales'] = $network_goods_data['fictitious_goods_sales'];
            }else{
                $goods_data['goods_sales'] = $network_goods_data['goods_sales'];
            }
            if($network_goods_data['status'] != 1){
                $goods_data['status'] = $network_goods_data['status'];
            }
            $data[$key]['goods_data'] = $goods_data;
        }

        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$data]);

    }
      
      /**
     * Get a JWT via given credentials.
     *  获取购物车数量
     * @return \Illuminate\Http\JsonResponse
     */

    public function Count(Request $request)
    {
        $openid = $request->post('openid');
        
        $user_data = UserModel::where(['openid'=>$openid])->first();
        
        if(empty($user_data)){
            return response()->json(['code'=>'401','msg' => '请登录']);
        }
        
        $data['shopping_cart_count'] = ShoppingCartModel::where(['user_id'=>$user_data['id']])->count();

    
        return response()->json(['code'=>'200','msg' => '请求成功','data'=>$data]);

    }
}


